﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApplication5.Models;
using WpfApplication5.ViewModels;

namespace WpfApplication5
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            SQLiteDatabase db = new SQLiteDatabase(new SQLiteHelper());
            var topCategories = db.ParentLoad(0);
            FunctionTreeViewModel vm = new FunctionTreeViewModel(topCategories, db);
            this.xTreeView.DataContext = vm;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(((sender as Button).Content as TextBlock).Text);
        }

        private void OnMouseLeftButtonDown(object sender, RoutedEventArgs e)
        {

        }
    }
    class TreeViewLineConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            TreeViewItem item = (TreeViewItem)value;
            ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item);
            return ic.ItemContainerGenerator.IndexFromContainer(item) == ic.Items.Count - 1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
    public class SampleFunction
    {
        public string Tag { get; set; }
        public string Sample { get; set; }
    }
    public class FunctionSampleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                List<SampleFunction> ltResult = new List<SampleFunction>();
                var samples= ((string)value).Split(';');
                foreach (var item in samples)
                {
                    var sample = new SampleFunction { Tag = item.Substring(1, 3), Sample = item };
                    ltResult.Add(sample);
                }
                return ltResult;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class ParamerAAAA
    {
        public string No { get; set; }
        public string ParameterInfo { get; set; }
    }
    public class FunctionParamerConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            dynamic parameters = value;
            //ObservableCollection<FunctionParameter> parameters = value as ObservableCollection<FunctionParameter>;
            //if (parameter != null)
            {
                List<ParamerAAAA> ltResult = new List<ParamerAAAA>();
                int index = 1;
                foreach (FunctionParameter p in parameters)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append(p.Name);
                    sb.Append("-");
                    sb.Append(p.DescCN);
                    sb.Append(p.VDescCN);
                    ltResult.Add(new ParamerAAAA { No = index++.ToString() + ".", ParameterInfo = sb.ToString() });
                }
                return ltResult;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
